<template>
  <form class="validate-form-container">
    <slot></slot>
    <div class="submit-area" >
      <slot name="submit">
        <button @click.prevent="handleSubmit" class="btn btn-primary">提交</button>
      </slot>
    </div>
  </form>
</template>

<script lang="ts">
import { defineComponent, onUnmounted } from 'vue'
import mitt, { Emitter } from 'mitt'
type ValidateFunc = ()=>boolean;
type Events = {
  'form-item-created':()=>boolean;
};
export const emitter:Emitter<Events> = mitt<Events>()
export default defineComponent({
  name: 'ValidateForm',
  emits: ['form-submit'],
  setup (props, context) {
    let funcArr:ValidateFunc[] = []
    const handleSubmit = () => {
      context.emit('form-submit', true)
    }
    const callback = (res:()=>boolean) => {
      funcArr.push(res)
    }
    emitter.on('form-item-created', callback)
    onUnmounted(() => {
      emitter.off('form-item-created', callback)
      funcArr = []
    })
    return {
      handleSubmit
    }
  }
})
</script>

<style scoped>

</style>
